package com.chess;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Index extends JFrame {
	private static final long serialVersionUID = 1L;
	Screen screen = new Screen();
	private int width = 600;
	private int height = 750;
	
	public Index(){
		this.setSize(this.width, this.height);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setResizable(false);
		this.add(screen);
		this.setTitle(" AI Chinese chess 3.0");
		
		screen.addMouseListener(new MyMouseAdapter());
	}
	
	class MyMouseAdapter extends MouseAdapter{
		@Override
		public void mouseReleased(MouseEvent e) {
			Action.ClickHandler(Util.getClickPosi(e.getX(), e.getY()));
			
			screen.repaint();
			
			//AI计算位置
			if(Util.curPlayer != Util.playerSide){
				new AI(Util.chesslist, Util.curPlayer).run();
			}
		}
	}
	
	class Screen extends JPanel{
		private static final long serialVersionUID = 1L;
		public int ChessX = 0;
		public int ChessY = 0;

		@Override
		public void paint(Graphics g) {
			super.paint(g);
			g.setColor(Color.LIGHT_GRAY);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			
			//画棋盘
			BufferedImage board = Util.imgs.get("board.jpg");
			int borderWidth = width;
			int borderHeight = 0;
			double ratio = (double)board.getWidth()/(double)board.getHeight();
			borderHeight = (int)(borderWidth/ratio);
			g.drawImage(board, 0, 0, borderWidth, borderHeight, this);
			
			//画出所有棋子
			for (Chess chess : Util.chesslist) {
				g.drawImage(chess.getImg(), Util.posi[chess.getX()][chess.getY()][0], Util.posi[chess.getX()][chess.getY()][1], Chess.SIZE, Chess.SIZE, this);
			}

			//画出可移动位置
			g.setColor(Color.RED);
			for (Integer[] coo : Util.movePosi) {
				g.fillOval(Util.posi[coo[0]][coo[1]][0] + 15, Util.posi[coo[0]][coo[1]][1] + 15, 19, 19);
			}
		}
	}

	public static void main(String[] args) {
		new Index();
	}
}
